package vlms.servlet;

import java.io.IOException;
import java.util.StringTokenizer;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import vlms.bean.IssuedMovie;
import vlms.bean.Movie;
import vlms.bean.Person;
import vlms.bean.SimpleCustomer;
import vlms.service.VLMSServiceProxy;
import java.text.*; 
import java.util.*; 

/**
 * Servlet implementation class ReturnMovie
 */
public class ReturnMovie extends HttpServlet {
	private static final long serialVersionUID = 1L;
	VLMSServiceProxy proxy = new VLMSServiceProxy();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ReturnMovie() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
proxy.setEndpoint("http://localhost:8080/VLMS/services/VLMSService");
		try
		{
		HttpSession session = request.getSession(true);
		
		String memNumber = request.getParameter("memNumber");
		System.out.println("member number" + memNumber);
		
		String result[]=proxy.retrieveIssuedMoviesByMemNumber(memNumber);
		
		if(result !=null)
		{
			
			int index=0;
			Movie[] tempMovieList=null;
			IssuedMovie[] tempIssuedMovieList=null;
			tempMovieList=new Movie[result.length];
			tempIssuedMovieList=new IssuedMovie[result.length];
			for(String temp: result)
			{
			
			StringTokenizer str=new StringTokenizer(temp,":");
			System.out.println(str.countTokens());
			if(str.countTokens()==8)
			{
				
				tempMovieList[index]=new Movie();
				tempIssuedMovieList[index]=new IssuedMovie();
				tempIssuedMovieList[index].setIssueid(Integer.parseInt(str.nextToken()));
				tempIssuedMovieList[index].setPersonid(Integer.parseInt(str.nextToken()));
				tempIssuedMovieList[index].setMovieid(Integer.parseInt(str.nextToken()));
				tempMovieList[index].setMoviename(str.nextToken());
				tempMovieList[index].setCategory(str.nextToken());
				tempMovieList[index].setRentamount(Float.parseFloat(str.nextToken()));
				
				tempIssuedMovieList[index].setIssuedstatus(str.nextToken());
				str.nextToken();
				
				
				index++;
			}	
			}
			for(Movie movie : tempMovieList)
			{
				System.out.println(movie.getMoviename());
			}
			for(IssuedMovie issuemovie : tempIssuedMovieList)
			{
				System.out.println(issuemovie.getMovieid());
			}
			
			if(tempMovieList!=null && tempIssuedMovieList !=null)
			{
				request.setAttribute("MovieList", tempMovieList);
				request.setAttribute("IssuedMovieList",tempIssuedMovieList);
				session.setAttribute("IssuedList", tempIssuedMovieList);
				RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/returnMovie.jsp");
				dispatcher.forward(request, response);
			}
			else
			{
				RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/returnMovie.jsp");
				dispatcher.forward(request, response);
			}
		
		}
		}
		catch(Exception e)
		{
			String message=e.getMessage();
			e.printStackTrace();
			request.setAttribute("message", message);
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/returnMovie.jsp");
			dispatcher.forward(request, response);
		}
		
	}

}
